﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Client;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using System.Collections.Generic;

namespace MangoMes.Write.API.Applications.Pickinges
{
    public class PickingUpdateHandler : IRequestHandler<PickingUpdateCommand, ResultsDTO>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBateRepository<Picking> PickingRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="PickingRepository"></param>
        public PickingUpdateHandler(IBateRepository<Picking> PickingRepository)
        {
            this.PickingRepository = PickingRepository;
        }
        #endregion

        #region 生产领料 修改
        /// <summary>
        /// 生产领料 修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO> Handle(PickingUpdateCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO对象result，并设置Code为 0
                ResultsDTO result = new ResultsDTO() { Code = 0 };
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper helper = new DapperHelper();
                //创建新的实例 picking
                Picking picking = new Picking();

                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                var str = "UPDATE picking SET PickingNode='" + request.PickingNode + "',PickingName='" + request.PickingName + "',PickingTime='" + request.PickingTime + "',WorkStationName='" + request.WorkStationName + "',ClientName='" + request.ClientName + "',Remark='" + request.Remark + "',Documents='" + request.Documents + "',ClientNo='" + request.ClientNo + "',WorkStations='" + request.WorkStations + "',ProductionWorkOrdersNo='" + request.ProductionWorkOrdersNo + "'WHERE PickingId=" + request.PickingId + "";

                var getnum = PickingRepository.Add(str, picking, conn);
                if (getnum != null)
                {
                    result.Code = 1; //如果getnum!=null，表示修改成功
                }

                //UPDATE picking SET PickingNode='"++"',

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
